package com.jinlejia.bbs.ui.mine;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsFragmentMineBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.mine.MineLogic;

/**
 * 我的 - 论坛
 */
public class BBSMineFragment extends Fragment implements MineLogic.LogicCallBack {


    public BBSMineFragment() {
        // Required empty public constructor
    }


    private View view;
    private BbsFragmentMineBinding binding;
    private MineLogic logic;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null){
            view =inflater.inflate(R.layout.bbs_fragment_mine, container, false);
            binding = DataBindingUtil.bind(view);
        }
        logic = new MineLogic(this,this);
        binding.setEvent(this);
        binding.setCommonEvent(CommonEvent.obtain());

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        logic.execute();
    }

    @Override
    public void onResult(BBSUserBean userBean) {
        if (userBean==null)return;
        Glide.with(getContext())
                .load(userBean.getUserAvatar())
                .dontAnimate()
                .placeholder(R.mipmap.bbs_user_no_avatar)
                .fallback(R.mipmap.bbs_user_no_avatar)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        binding.circleImageView.setImageDrawable(resource); //显示图片
                    }
                });
        binding.setUser(userBean);
    }

    //region 编辑自己的信息
    private final int REQUEST_EDIT_INFO = 10;
    public void editMineInfo(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSEditMineInfoActivity.class);
        startActivityForResult(intent,REQUEST_EDIT_INFO);
    }

    private void updateUserInfo(){

    }
    //endregion

    //跳转至 我的收藏
    public void toCollectActivity(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSCollectActivity.class);
        startActivity(intent);
    }

    //跳转至 邀请回答页面
    public void toInviteAnswer(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSInviteAnswerActivity.class);
        startActivity(intent);
    }
    //跳转至 我的关注
    public void toAttentionActivity(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSAttentionActivity.class);
        startActivity(intent);
    }

    //跳转至 我的提问
    public void toMyQuizActivity(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSMyQuizActivity.class);
        startActivity(intent);
    }

    //跳转至 我的回答
    public void toMyAnswerActivity(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSMineAnswerListActivity.class);
        startActivity(intent);
    }

    public void toMyFansActivity(View view){
        Intent intent = new Intent();
        intent.setClass(getContext(),BBSMyFansActivity.class);
        startActivity(intent);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQUEST_EDIT_INFO:
                updateUserInfo();
                break;
        }
    }
}
